package com.example.datn.activities;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;


import com.example.datn.adapter.*;

import com.example.datn.funcition.*;
import com.example.datn.R;


public class PhotoActivity extends Activity{
	private int albumso;
	JSONObject jsonobject;
	JSONArray jsonarray;
	ListView listview;
	ListImageAdapter adapter;
	ProgressDialog mProgressDialog;
	ArrayList<HashMap<String, String>> arraylist;
	public static String RANK = "rank";
	
	public static String FLAG = "flag";
@Override
protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.list_photo);
	
	Bundle bundle= getIntent().getExtras();
	
	albumso=bundle.getInt("a");
	
	System.out.println("albummmmmmmmmmm:  "+albumso);
	new DownloadJSON().execute();
}

//DownloadJSON AsyncTask
	private class DownloadJSON extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			// Create a progressdialog
			mProgressDialog = new ProgressDialog(PhotoActivity.this);
			// Set progressdialog title
			mProgressDialog.setTitle("Loading....");
			// Set progressdialog message
			mProgressDialog.setMessage("Loading...");
			mProgressDialog.setIndeterminate(false);
			// Show progressdialog
			mProgressDialog.show();
		}

		@Override
		protected Void doInBackground(Void... params) {
			// Create an array
			arraylist = new ArrayList<HashMap<String, String>>();
			// Retrieve JSON Objects from the given URL address
			jsonobject = JSONfunctions
					.getJSONfromURL("http://thanhandha.byethost7.com/websevice.php?type=albumphotoquanglephuongmychi");

			try {
				// Locate the array name in JSON
				jsonarray = jsonobject.getJSONArray("photos");

				for (int i = 0; i < jsonarray.length(); i++) {
					HashMap<String, String> map = new HashMap<String, String>();
					jsonobject = jsonarray.getJSONObject(i);
					// Retrive JSON Objects
					map.put("rank", jsonobject.getString("id"));
					map.put("flag", jsonobject.getString("photo"));
					// Set the JSON Objects into the array
					arraylist.add(map);
				}
			} catch (JSONException e) {
				Log.e("Error", e.getMessage());
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void args) {
			// Locate the listview in listview_main.xml
			listview = (ListView) findViewById(R.id.listview);
			// Pass the results into ListViewAdapter.java
			adapter = new ListImageAdapter(PhotoActivity.this, arraylist);
			// Set the adapter to the ListView
			listview.setAdapter(adapter);
			// Close the progressdialog
			mProgressDialog.dismiss();
		}
	}
}